import React from 'react';
import ReviewList from './plugins/ReviewList';
import ReviewContent from './plugins/ReviewContent';

import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import * as reviewAction from '../../../actions/review.action' 

import {isEqual} from 'lodash';
import { u } from '../../../actions/util'


@connect(({ app, review }) => ({ app, review }), dispatch => ({actions: bindActionCreators({...reviewAction}, dispatch)}))
class ReviewIndex extends React.Component {
  static contextTypes = {
    router: React.PropTypes.object
  }
  constructor(props) {
    super(props);
    this.displayName = 'ReviewIndex';
    this.state = {
      reviewItem: null,
      activities: null,
      reviewer: null,
      reviewerM: null,
    }
  }
  componentDidMount() {
    this.props.actions.fetchReviewList({
      statuses: '2,3',
      pageNumber: 1,
      pageSize: 10000,
      type: 1,
    }, (data) => {
      const {
        itemId
      } = this.props.routeParams;
      if(itemId === 'default'){
        const list = data.reviewTemplates;
        //异步 使cdm走完
        setTimeout(()=>{
          if(list.length) this.context.router.replace(`/review/index/${list[0].templateId}`);
        }, 0)
      }else{
        this.getReviewItem(itemId, data);
      }
    });
  }
  componentWillReceiveProps(nextProps) {
    const {
      itemId
    } = this.props.routeParams;
    const {
      itemId: nextItemId
    } = nextProps.routeParams;
    if(itemId !== nextItemId) {
      const templates = nextProps.review.templates;
      const list = templates.reviewTemplates;
      const item = list.find(v => v.templateId == nextItemId);
      if(item)
        this.getReviewItem(nextItemId, templates);
      else
        setTimeout(()=>{
          if(list.length) this.context.router.replace(`/review/index/${list[0].templateId}`);
        }, 0)
    }
  }
  componentWillUnmount() {
    this.props.actions.cleanReviewList();
  }
  getReviewItem = (_itemId, _templates) => {
    const templates = _templates || this.props.review.templates;
    const itemId = _itemId || this.props.routeParams.itemId;
    const item = templates.reviewTemplates.find(v => v.templateId == itemId);
    this.setState({
      reviewItem: item,
      activities: null,
      reviewer: null,
      reviewerM: null,
    });
    this.props.actions.getActivities({templateId: itemId, userId: u.userId}, (data) => {
      if(this.props.routeParams.itemId === itemId) {
        this.setState({
          activities: data || {},
        });
      }
    });
    this.props.actions.getReviewer({templateId: itemId, userId: u.userId, isManager: '0'}, (data) => {
      if(this.props.routeParams.itemId === itemId) {
        this.setState({
          reviewer: data.reviewInvitations || [],
        }); 
      }
    });
    this.props.actions.getReviewer({templateId: itemId, userId: u.userId, isManager: '1'}, (data) => {
      if(this.props.routeParams.itemId === itemId) {
        this.setState({
          reviewerM: data.reviewInvitations || [],
        });
      }
    });
  }
  render() {
    const item = this.state.reviewItem;
    const {
      templates,
    } = this.props.review;
    return (
      <div>
        <ReviewList listParms={this.props.review.templates} routeParams={this.props.routeParams} profile={this.props.app.profile} />
        <ReviewContent item={item || null} activities={this.state.activities} reviewer={this.state.reviewer} reviewerManager={this.state.reviewerM} />
      </div>
    );
  }
}

export default ReviewIndex;
